/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved.
 */
package com.kaaction.modules.eb.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.List;

import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;

import com.kaaction.common.persistence.DataEntity;
import com.kaaction.modules.sys.entity.Menu;

/**
 * 商品分类Entity
 * @author huanghaiming
 * @version 2017-04-11
 */
public class CommerceClassify extends DataEntity<CommerceClassify> {
	
	private static final long serialVersionUID = 1L;
	private CommerceClassify parent;		// parent_id
	private String parentIds;		// parent_ids
	private String name;		// name
	
	public CommerceClassify() {
		super();
	}

	public CommerceClassify(String id){
		super(id);
	}

	@JsonBackReference
	@NotNull(message="parent_id不能为空")
	public CommerceClassify getParent() {
		return parent;
	}

	public void setParent(CommerceClassify parent) {
		this.parent = parent;
	}
	
	@Length(min=0, max=2000, message="parent_ids长度必须介于 0 和 2000 之间")
	public String getParentIds() {
		return parentIds;
	}

	public void setParentIds(String parentIds) {
		this.parentIds = parentIds;
	}
	
	@Length(min=1, max=255, message="name长度必须介于 1 和 255 之间")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getParentId() {
		return parent != null && parent.getId() != null ? parent.getId() : "0";
	}
	
	
	@JsonIgnore
	public static void sortList(List<CommerceClassify> list, List<CommerceClassify> sourcelist, String parentId, boolean cascade){
		for (int i=0; i<sourcelist.size(); i++){
			CommerceClassify e = sourcelist.get(i);
			if (e.getParent()!=null && e.getParent().getId()!=null
					&& e.getParent().getId().equals(parentId)){
				list.add(e);
				if (cascade){
					// 判断是否还有子节点, 有则继续获取子节点
					for (int j=0; j<sourcelist.size(); j++){
						CommerceClassify child = sourcelist.get(j);
						if (child.getParent()!=null && child.getParent().getId()!=null
								&& child.getParent().getId().equals(e.getId())){
							sortList(list, sourcelist, e.getId(), true);
							break;
						}
					}
				}
			}
		}
	}

	@JsonIgnore
	public static String getRootId(){
		return "1";
	}
	
	@Override
	public String toString() {
		return "CommerceClassify [parent=" + parent  + ", name=" + name +"]";
	}
	
	
	
}